package com.sqkj.controller;

import com.sqkj.dao.ArticleDao;
import com.sqkj.dao.ArticleStarDao;
import com.sqkj.entity.Article;
import com.sqkj.entity.ArticleStar;
import com.sqkj.service.ArticleService;
import com.sqkj.service.ArticleStarService;
import com.sqkj.service.UserService;
import jdk.net.SocketFlow;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.rmi.MarshalledObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ArticleStar")
public class ArticleStarController {
     @Resource
    ArticleStarService service;
     @Resource
    ArticleService articleService;
     @Resource
    UserService userService;
    @RequestMapping("/addStar")
    public Map<String, Object> addStar(String openId,int articleId) {
        Boolean isSuccess = false;
        Map<String, Object> map = new HashMap<String, Object>();
        int userId = userService.findByOpenId(openId).getId();
        Boolean isExsit = service.findByUserIdAndArticleId(userId, articleId);
        if (isExsit) {
            isSuccess = service.giveStar(userId, articleId);
        }else {
            ArticleStar star = new ArticleStar(userId,articleId,1);
            isSuccess = service.addStar(star);
        }

        int StarCount = articleService.findById(articleId).getArticleGoodCnt();
        articleService.addGoodCut(StarCount + 1, articleId);
        map.put("isSuccess", isSuccess);
        return map;
    }
    @RequestMapping("/removeStar")
    public Map<String, Object> removeStar(String openId , int articleId) {
        int userId = userService.findByOpenId(openId).getId();
        Map<String, Object> map = new HashMap<String, Object>();
        Boolean isSuccess = service.removeStar(userId, articleId);
        int StarCount = articleService.findById(articleId).getArticleGoodCnt();
        articleService.reduceGoodCut(StarCount - 1, articleId);
        map.put("isSuccess", isSuccess);
        return map;
    }

    @RequestMapping("/findStarIsExsit")
    public Boolean findStarIsExsit(String openId, int articleId) {
        int userId = userService.findByOpenId(openId).getId();
        return service.findByUserIdAndArticleId(userId,articleId);
    }
    @RequestMapping("/getStarStatus")
    public int GetStarStatus(String openId , int articleId) {
        int userId = userService.findByOpenId(openId).getId();
        return service.findStarStatus(userId, articleId);
    }
    @RequestMapping("/findMyselfStar")
    public Map<String, Object> findMyselfStar(int userId) {
        HashMap<String, Object> map = new HashMap<>();
        List<Article> articleList = new ArrayList<>();
        List<Integer> list = service.findMyselfStar(userId);
        for (int id:list) {
            articleList.add(articleService.findById(id));
        }

        map.put("articleList", articleList);
        return map;
    }
}
